package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;

import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.service.ISysRoleMenuService;
import com.itheima.ydd.service.ISysRoleService;
import com.itheima.ydd.service.ISysUserService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_role")
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;
    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/list")
    public R list(Long page, long limit, String name) {
        //分装查询条件
        Page<SysRole> sysRolePage = new Page<>(page, limit);
        LambdaQueryWrapper<SysRole> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name), SysRole::getName, name);
        sysRoleService.page(sysRolePage, qw);
        // 返回封装对象
        R r = R.success("count", sysRolePage.getTotal());
        r.putData("pageSize", sysRolePage.getSize());
        r.putData(" currentPage", sysRolePage.getCurrent());
        r.putData("totalPage", sysRolePage.getTotal());
        ArrayList<Map> list = new ArrayList<Map>();
        //遍历查询分页
        for (SysRole record : sysRolePage.getRecords()) {
            HashMap<String, Object> maps = new HashMap<>();
            maps.put("id", record.getId());
            maps.put("name", record.getName());
            maps.put("description", record.getDescription());
            maps.put("create_date", record.getCreateDate());
            maps.put("update_date", record.getUpdateDate());
            maps.put("remarks", record.getRemarks());
            maps.put("del_flag", record.getDelFlag());
            maps.put("superadmin", record.getSuperadmin());
            maps.put("default_data", record.getDefaultData());
            maps.put("role_id", record.getId());
            //根据id查询配置
            LambdaQueryWrapper<SysRoleMenu> mqw = new LambdaQueryWrapper<>();
            mqw.eq(SysRoleMenu::getRoleId, record.getId());
            List<SysRoleMenu> list1 = sysRoleMenuService.list(mqw);
            ArrayList<String> str = new ArrayList<>();
            for (SysRoleMenu sysRoleMenu : list1) {
                str.add(sysRoleMenu.getMenuId());
            }
            String roleMenus = StringUtils.join(str, ",");

            maps.put("role_menus", roleMenus);
            list.add(maps);
        }

        r.putData("data", list);
        return r;

    }  /**
     * 角色修改
     *
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(String id) {
        //SysRoleDto sysRole = (SysRoleDto) sysRoleService.getById(id);
        SysRoleDto sysRole = sysRoleService.getByCondition(id);
        return ResultData.ok("", sysRole);
    }

    /**
     * 修改角色
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody SysRoleDto sysRoleDto) {

        sysRoleService.updateById(sysRoleDto);
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId, sysRoleDto.getId());
        sysRoleMenuService.remove(queryWrapper);
        List<String> menuids = sysRoleDto.getMenuids();
        for (String menuid : menuids) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuid);
            sysRoleMenu.setRoleId(sysRoleDto.getId());
            sysRoleMenuService.save(sysRoleMenu);
        }
        return ResultData.ok("修改成功", "");
    }

    /**
     * 角色添加
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody SysRoleDto sysRoleDto) {
     sysRoleService.add(sysRoleDto);
        return ResultData.ok("角色添加成功", sysRoleDto);
    }

    /**
     * 删除角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData deleteById(String id) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getId, id);
        queryWrapper.eq(SysRole::getDefaultData, 1);
        SysRole sysRole = sysRoleService.getOne(queryWrapper);
        if (sysRole != null) {
            return ResultData.error("系统默认配置，无法删除");
        }
        //删除角色
        sysRoleService.removeById(id);
        return ResultData.ok("删除成功", null);
    }

    /**
     * 所有管理员数据
     *
     * @return
     */
    @GetMapping("/allData")
    public ResultData allData() {
        List<SysRole> list = sysRoleService.list();
        return ResultData.ok("", list);
    }
}